package database

import (
	"fmt"
	"z-blog/models"
	"z-blog/utils"
)

func FirstOpen() {
	var userList []User
	db.Find(&userList)
	if len(userList) == 0 {
		user := User{
			Username: "admin",
			Password: "admin",
			Role:     true,
		}
		//创建初始管理用户
		db.Create(&user)
		fmt.Println("初始管理员用户与密码为：admin")
	}
}

func UserLogin(username, password string) (bool, string) {
	var users []User
	db.Where("username = ? AND password = ?", username, password).Find(&users)
	if len(users) > 0 {
		//生成随机数
		token := utils.RandomNum()
		db.Model(&User{}).Where("username = ?", username).Update("token", token)
		return true, token
	}
	return false, ""
}

func AuthToken(username, token string) bool {
	var users []User
	db.Where("username = ? AND token = ?", username, token).Find(&users)
	if len(users) > 0 {
		return true
	}
	return false
}

func AddSort(urlName, menuName string) bool {
	var tmps []Sort
	//查询数据是否重复
	db.Where("url_name = ? AND name = ?", urlName, menuName).Find(&tmps)
	if len(tmps) > 0 {
		return false
	} else {
		//查询全部全部分类
		var sorts []Sort
		db.Model(&Sort{}).Find(&sorts)
		sort := Sort{
			SortIndex: len(sorts) + 1,
			UrlName:   urlName,
			Name:      menuName,
		}
		db.Create(&sort)
		return true
	}
}

func GetSorts() []Sort {
	var sorts []Sort
	db.Order("sort_index asc").Find(&sorts)
	return sorts
}

func DeleteSort(name string) []Sort {
	var sort Sort
	var sorts []Sort
	db.Where("name = ?", name).Delete(&sort)
	db.Model(&Sort{}).Find(&sorts)
	return sorts
}

// SortMenu 更新分类排序
func SortMenu(data []models.List) []Sort {
	//查询所有分类
	var sorts []Sort
	var resSort []Sort
	db.Model(&Sort{}).Find(&sorts)
	for i := 0; i < len(sorts); i++ {
		for _, item := range data {
			if item.Name == sorts[i].Name {
				db.Model(&Sort{}).Where("name = ?", item.Name).Update("sort_index", item.Index)
				break
			}
		}
	}
	db.Order("sort_index asc").Find(&resSort)
	return resSort
}

// ReleaseArticle 发布文章
func ReleaseArticle(data models.ArticleContentModel, user string) error {
	content := ArticleContent{
		Title:   data.Title,
		Class:   data.Class,
		Content: data.Content,
		Author:  user,
	}
	err := db.Create(&content).Error
	if err != nil {
		return err
	}
	return nil
}

func GetArticleList() []ArticleContentList {
	var data []ArticleContentList
	db.Model(&ArticleContent{}).Select("id", "title", "class", "content", "author").Find(&data)
	return data
}

// GetArticleDetails 获取文章内容
func GetArticleDetails(id int) ArticleContent {
	var data ArticleContent
	db.Where("id = ?", id).Find(&data)
	return data
}

// PagingFind 分页查询
func PagingFind(data models.PagingFindModel) ([]ArticleContentList, int) {
	var articleContentList []ArticleContentList
	var totalCon []ArticleContent
	db.Model(&ArticleContent{}).Limit(data.PagesSize).Offset((data.ThisPage - 1) * data.PagesSize).Find(&articleContentList)
	db.Find(&totalCon)
	total := len(totalCon)
	return articleContentList, total
}

// DeleteArticle 删除文章
func DeleteArticle(data models.DeleteArticleModel) {
	db.Where("id = ? AND title = ?", data.Id, data.Title).Delete(&ArticleContent{})
}

// FindArticleInfo 查询文章内容
func FindArticleInfo(id int, title string) string {
	var data ArticleContent
	db.Where("id = ? AND title = ?", id, title).First(&data)
	return data.Content
}

// UpdateArticle 更新文章
func UpdateArticle(data models.UpdateArticleModel) error {
	result := db.Where("id = ?", data.ID).Updates(ArticleContent{Title: data.Title, Class: data.Class, Content: data.Content})
	return result.Error
}

// GetClassifyList 根据分类查询文章列表
func GetClassifyList(class models.ClassifyListModel) []ArticleContentList {
	var data []ArticleContentList
	db.Model(&ArticleContent{}).Where("class = ?", class.ClassifyName).Find(&data)
	return data
}

// GetClassifyPaging 分类分页查询
func GetClassifyPaging(data models.GetClassifyPagingModel) ([]ArticleContentList, int) {
	var articleContentList []ArticleContentList
	var total []ArticleContent
	db.Model(&ArticleContent{}).Where("class = ?", data.Type).Limit(data.PagesSize).Offset((data.ThisPage - 1) * data.PagesSize).Find(&articleContentList)
	db.Where("class = ?", data.Type).Find(&total)
	return articleContentList, len(total)
}

// SearchArticle 搜索文章
func SearchArticle(con string) []ArticleContentList {
	var articleContentList []ArticleContentList
	db.Model(&ArticleContent{}).Where("title LIKE ?", "%"+con+"%").Or("content LIKE ?", "%"+con+"%").Find(&articleContentList)
	return articleContentList
}
